package com.example.controller;

import com.example.pojo.Dept;
import com.example.pojo.MyResponseResult;
import com.example.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public MyResponseResult<List<Dept>> selectAll(){

        List<Dept> depts =deptService.selectAll();
        return MyResponseResult.ok(depts);
    }

    @DeleteMapping("/{id}")
    public MyResponseResult<Object> deleteById(@PathVariable Integer id) throws ParseException {
        int i =deptService.deleteById(id);
        return i>=1?MyResponseResult.ok("ok"):MyResponseResult.error(id+"删除失败，请确认id");
    }//删部门

    @PostMapping
    public MyResponseResult<Object> addDept(@RequestBody Dept dept){
        int i = deptService.addDept(dept);
        return i>=1?MyResponseResult.ok("ok"):MyResponseResult.error("添加失败");
    }
    @GetMapping("/{id}")
    public MyResponseResult<Object> selectById(@PathVariable Integer id){
        Dept dept = deptService.selectById(id);

        return dept!=null?MyResponseResult.ok(dept):MyResponseResult.error("查询失败");
    }
    @PutMapping
    public MyResponseResult<Object> updateDept(@RequestBody Dept dept){
       int i =deptService.updateDept(dept);
       return i>=1?MyResponseResult.ok("null"):MyResponseResult.error("修改失败");

    }

}
